package vn.edu.saigontech.ncms.util;

import java.security.*;

public class PasswordEncoder {
	public static final String ALGORITHM = "SHA";
	
	public static String encode(String password) {
		byte[] unencodedPassword = password.getBytes();

        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(ALGORITHM);
        } 
        catch (Exception ex) {
            return password;
        }

        md.reset();
        md.update(unencodedPassword);

        byte[] encodedPassword = md.digest();
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < encodedPassword.length; i++) {
            if ((encodedPassword[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
        }
        return buf.toString();
	}
}